﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.LichHocLopTinChiService;
namespace eTMS.Controllers{
public class LichHocLopTinChiControllerBase : CommonController{
	public LichHocLopTinChiControllerBase()
		: base()
	{	
		this.dao = new LichHocLopTinChiServiceClient();
	}
	~LichHocLopTinChiControllerBase(){
		dao.Close();
	}
	private DataTable _Table;
	private LichHocLopTinChiEntity oLichHocLopTinChi;	
	public LichHocLopTinChiEntity LichHocLopTinChiObject{
		get{
			return oLichHocLopTinChi;
		}
		set{
			oLichHocLopTinChi=value;
		}
	}
	protected LichHocLopTinChiServiceClient dao;
	public DataTable Table{
		get{
			return _Table;
		}
        set
        {
            _Table = value;
        }
	}
	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oLichHocLopTinChi==null)return false;
		bool result=true;
		try{
			oLichHocLopTinChi = dao.SaveRet(oLichHocLopTinChi);
		}
		catch{
			result=false;
		}
		if(result){
			_ListLichHocLopTinChi.Add(oLichHocLopTinChi);
		}
		return result;
	}
	public bool Update(){
		if(oLichHocLopTinChi==null)return false;
		return dao.Update(oLichHocLopTinChi);
	}
	public bool Delete(){
		if(oLichHocLopTinChi==null)return false;
		bool retvalue = dao.Delete(oLichHocLopTinChi);
		if(retvalue){			
			_ListLichHocLopTinChi.Remove(oLichHocLopTinChi);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 LichHocLopTinChi_ID){
		bool retvalue = dao.DeleteByID(LichHocLopTinChi_ID);		
		if(retvalue){
			LichHocLopTinChiEntity oDeleted=FindLichHocLopTinChi(LichHocLopTinChi_ID);
			if(oDeleted!=null){
				_ListLichHocLopTinChi.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oLichHocLopTinChi = new LichHocLopTinChiEntity();
	}
    //Copy data from data row in grid to object
    public LichHocLopTinChiEntity MapDataRowToObject(DataRow row){
		LichHocLopTinChiEntity o = new LichHocLopTinChiEntity();
        if (!row.IsNull("GiangVienLopTinChi_ID"))
             o.GiangVienLopTinChi_ID = (Int32)(row["GiangVienLopTinChi_ID"]);
		 if(!row.IsNull("PhongHoc_ID"))
			o.PhongHoc_ID=(Int32)(row["PhongHoc_ID"]);
		 if(!row.IsNull("ThuTrongTuan_ID"))
			o.ThuTrongTuan_ID=(Int32)(row["ThuTrongTuan_ID"]);
		 if(!row.IsNull("TietHoc_ID"))
			o.TietHoc_ID=(Int32)(row["TietHoc_ID"]);
		 if(!row.IsNull("TuanHoc_ID"))
			o.TuanHoc_ID=(Int32)(row["TuanHoc_ID"]);
		 if(!row.IsNull("LichHocLopTinChi_ID"))
		    o.LichHocLopTinChi_ID=(Int32)(row["LichHocLopTinChi_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(LichHocLopTinChiEntity o,DataRow row){
        row["GiangVienLopTinChi_ID"] = o.GiangVienLopTinChi_ID;
		row["PhongHoc_ID"]=o.PhongHoc_ID;
		row["ThuTrongTuan_ID"]=o.ThuTrongTuan_ID;
		row["TietHoc_ID"]=o.TietHoc_ID;
		row["TuanHoc_ID"]=o.TuanHoc_ID;
		row["LichHocLopTinChi_ID"]=o.LichHocLopTinChi_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.LichHocLopTinChiObject,row);
    }
	public void LoadObject(Int32 LichHocLopTinChi_ID){
		LichHocLopTinChiEntity oLoad=FindLichHocLopTinChi(LichHocLopTinChi_ID);
		if(oLoad==null){
			oLichHocLopTinChi=this.LoadLichHocLopTinChi(LichHocLopTinChi_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oLichHocLopTinChi.IsNullLichHocLopTinChi_ID;
			if(!bNotIsNull){
				this.ListLichHocLopTinChi.Add(oLichHocLopTinChi);
			}
		}
		else{
			oLichHocLopTinChi=oLoad;
		}
	}
	#region CachedList LichHocLopTinChi
	private static List<LichHocLopTinChiEntity> _ListLichHocLopTinChi=new List<LichHocLopTinChiEntity>();
    public List<LichHocLopTinChiEntity>ListLichHocLopTinChi{
		get{
			return _ListLichHocLopTinChi;
		}
	}
	public static List<LichHocLopTinChiEntity>ListLichHocLopTinChiStatic{
		get{
			return _ListLichHocLopTinChi;
		}
	}
	public LichHocLopTinChiEntity FindLichHocLopTinChi(Int32 LichHocLopTinChi_ID){
		LichHocLopTinChiEntity oResult=this.ListLichHocLopTinChi.Find(delegate(LichHocLopTinChiEntity oFind){
			bool result=true; 
			result&=oFind.LichHocLopTinChi_ID.Equals(LichHocLopTinChi_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllLichHocLopTinChi(bool clearCache){
		if(_ListLichHocLopTinChi.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		LichHocLopTinChiServiceClient dao = new LichHocLopTinChiServiceClient();
		_ListLichHocLopTinChi=new List<LichHocLopTinChiEntity>(dao.SelectAll());
		dao.Close();
	}
	public LichHocLopTinChiEntity LoadLichHocLopTinChi(Int32 LichHocLopTinChi_ID){			
		LichHocLopTinChiEntity o=dao.SelectOne(LichHocLopTinChi_ID);		
		return o;
	}
	#endregion
}
}
